package serviceImpl;

import DAO.BaseDAO;
import DTO.Merchaned;
import pageUtil.PageB;
import util.DBConnection;
import util.DBConnection.CallBack;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MerchanedDAO implements BaseDAO<Merchaned> {
    @Override
    public boolean add(Merchaned merchaned, String sql) throws Exception {
        Connection conn = DBConnection.getConn();
        boolean boo = DBConnection.executUpadte(conn, sql,
                merchaned.getName(),
                merchaned.getPhoto(),
                merchaned.getMoney(),
                merchaned.getLb()
        );
        if(conn!=null){
            conn.close();
        }
        return boo;
    }

    @Override
    public boolean delete(Merchaned merchaned) throws Exception {
        String sql = "delete from merchaned where id = ?";
        Connection conn  = DBConnection.getConn();
        boolean boo = DBConnection.executUpadte(conn, sql, merchaned.getId());
        if(conn!=null){
            conn.close();
        }
        return boo;
    }

    @Override
    public boolean update(Merchaned merchaned, String sql) throws Exception {
        Connection conn = DBConnection.getConn();
        boolean boo = DBConnection.executUpadte(conn, sql,
                merchaned.getName(),
                merchaned.getMoney(),
                merchaned.getLb(),
                merchaned.getId()
        );
        if(conn!=null){
            conn.close();
        }
        return boo;
    }

    @Override
    public List<Merchaned> findAll(int currentPage, int pageSize, String sql) throws Exception {
        return DBConnection.queryList(new CallBack<Merchaned>() {
            @Override
            public List<Merchaned> getDatas(ResultSet rs) {
            	Merchaned merchaned = null;
                List<Merchaned> list = new ArrayList<Merchaned>();
                try {
                    while(rs.next()){
                    	merchaned = new Merchaned();
                    	merchaned.setId(rs.getInt("id"));
                         merchaned.setName(rs.getString("name"));
                         merchaned.setPhoto(rs.getString("photo"));
                         merchaned.setMoney(rs.getString("money"));
                         merchaned.setLb(rs.getString("lb"));
                        list.add(merchaned);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs!=null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println(list);
                return list;

            }


        }, sql,currentPage,pageSize);
    }

    @Override
    public List<Merchaned> findAll(String sql) throws Exception {
        sql = "select a.id,a.name,a.photo,a.money,b.lbname as lb from merchaned a,lb b where a.lb = b.lbcs";
        return DBConnection.queryList(new CallBack<Merchaned>() {
            @SuppressWarnings("null")
			@Override
            public List<Merchaned> getDatas(ResultSet rs) {
                List<Merchaned> list = new ArrayList<Merchaned>();
                Merchaned merchaned = null;
                try {
                    while (rs.next()) {
                        int id = rs.getInt("id");
                        String name = rs.getString("name");
                        String photo = rs.getString("photo");
                        String money = rs.getString("money");
                        String lb = rs.getString("lb");
                        merchaned.setId(id);
                        merchaned.setName(name);
                        merchaned.setPhoto(photo);
                        merchaned.setMoney(money);
                        merchaned.setLb(lb);
                        list.add(merchaned);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println(list);
                return list;
            }
        }, sql);
    }

    @Override
    public Merchaned findById(String id) throws Exception {
        String sql = "select a.id,a.name,a.photo,a.money,b.lbname as lb from merchaned a,lb b where a.lb = b.lbcs and a.id = ?";
        return DBConnection.queryOne(new CallBack<Merchaned>() {
            @Override
            public Merchaned getData(ResultSet rs) {
            	Merchaned merchaned = null;
                try {
                    if(rs.next()){
                        merchaned = new Merchaned();
                        merchaned.setId(rs.getInt("id"));
                        merchaned.setName(rs.getString("name"));
                        merchaned.setPhoto(rs.getString("photo"));
                        merchaned.setMoney(rs.getString("money"));
                        merchaned.setLb(rs.getString("lb"));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs!= null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return merchaned;
            }


        }, sql, id);
    }

    @Override
    public Merchaned findByPropert(Merchaned merchaned) throws Exception {
    	String sql = "select id,name,photo,money,lb from merchaned where id = ?";
        return DBConnection.queryOne(new CallBack<Merchaned>() {
            @Override
            public Merchaned getData(ResultSet rs) {
            	Merchaned merchaned = null;
                try {
                    if(rs.next()){
                        merchaned = new Merchaned();
                        merchaned.setId(rs.getInt("id"));
                        merchaned.setName(rs.getString("name"));
                        merchaned.setPhoto(rs.getString("photo"));
                        merchaned.setMoney(rs.getString("money"));
                        merchaned.setLb(rs.getString("lb"));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs!= null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return merchaned;
            }


        }, sql, merchaned.getId());
    }

    @Override
    public int dataCount(String sql) throws Exception {
    	return DBConnection.queryOne(new CallBack<Integer>() {
            @Override
            public Integer getData(ResultSet rs) {
                int count = 0;
                try {
                    if(rs.next()){
                        count = rs.getInt(1);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs!=null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return count;

            }
        }, sql);
    }

    @Override
    public List<Merchaned> findByPage(PageB<Merchaned> bean, String sql) throws SQLException {
    	int start = (bean.getCurrentPage()-1)*bean.getPageSize();
        return DBConnection.queryList(new CallBack<Merchaned>() {

            @Override
            public List<Merchaned> getDatas(ResultSet rs) {
                List<Merchaned> list = new  ArrayList<Merchaned>();
                Merchaned merchaned = null;
                try {
                    while(rs.next()){
                    	merchaned = new Merchaned();
                    	merchaned.setId(rs.getInt("id"));
                         merchaned.setName(rs.getString("name"));
                         merchaned.setPhoto(rs.getString("photo"));
                         merchaned.setMoney(rs.getString("money"));
                         merchaned.setLb(rs.getString("lb"));
                        list.add(merchaned);
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }finally{
                    if(rs!=null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }

                return list;
            }

        }, sql, start,bean.getPageSize());
    }
}
